cmake_minimum_required(VERSION 3.16)

project(FaceAttendence VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 交叉编译配置
if(CMAKE_CROSSCOMPILING)
    # 设置交叉编译工具链
    set(CMAKE_SYSTEM_NAME Linux)
    set(CMAKE_SYSTEM_PROCESSOR arm)
    
    # 设置交叉编译器路径
    set(CMAKE_C_COMPILER arm-linux-gnueabihf-gcc)
    set(CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++)
    
    # 设置工具链路径环境变量
    set(ENV{PATH} "/usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin:$ENV{PATH}")
    
    # 设置根文件系统路径
    set(CMAKE_FIND_ROOT_PATH /home/nfs_root)
    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
endif()

# 根据编译环境设置不同的路径
if(CMAKE_CROSSCOMPILING)
    # 交叉编译环境路径
    set(PACKAGE_ROOT "/home/package")
    set(NFS_ROOT "/home/nfs_root")
    
    # OpenCV路径
    set(OPENCV_ROOT "${PACKAGE_ROOT}/opencv-4.5.2")
    set(OPENCV_CONTRIB_ROOT "${PACKAGE_ROOT}/opencv_contrib-4.5.2")
    
    # SeetaFace路径
    set(SEETAFACE_ROOT "${PACKAGE_ROOT}/SeetaFace2")
    
    # Qt路径（交叉编译版本）
    set(CMAKE_PREFIX_PATH "${NFS_ROOT}/qt" ${CMAKE_PREFIX_PATH})
    set(Qt6_DIR "${NFS_ROOT}/qt/lib/cmake/Qt6")
else()
    # Windows开发环境路径（保持原有配置）
    set(CMAKE_PREFIX_PATH "E:/downloads/code/QT/6.8.3/mingw_64" ${CMAKE_PREFIX_PATH})
    set(Qt6_DIR "E:/downloads/code/QT/6.8.3/mingw_64/lib/cmake/Qt6")
    
    set(OPENCV_ROOT "E:/code/Qtproject/package/opencv452")
    set(SEETAFACE_ROOT "E:/code/Qtproject/package/SeetaFace")
endif()

# 根据编译环境选择Qt版本
if(CMAKE_CROSSCOMPILING)
    # 交叉编译环境使用Qt5
    find_package(Qt5 REQUIRED COMPONENTS Widgets Network)
    set(QT_VERSION_MAJOR 5)
else()
    # Windows环境使用Qt6
    find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets Network)
    find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Network)
endif()

set(PROJECT_SOURCES
        main.cpp
        faceattendence.cpp
        faceattendence.h
        faceattendence.ui
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(FaceAttendence
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
        pic.qrc
    )
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET FaceAttendence APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
    if(ANDROID)
        add_library(FaceAttendence SHARED
            ${PROJECT_SOURCES}
        )
# Define properties for Android with Qt 5 after find_package() calls as:
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else()
        add_executable(FaceAttendence
            ${PROJECT_SOURCES}
        )
    endif()
endif()

# 添加头文件目录
if(CMAKE_CROSSCOMPILING)
    include_directories(
        "${OPENCV_ROOT}/include"
        "${OPENCV_ROOT}/include/opencv4"
        "${OPENCV_ROOT}/include/opencv4/opencv2"
        "${OPENCV_CONTRIB_ROOT}/modules/face/include"
        "${SEETAFACE_ROOT}/include"
        "${SEETAFACE_ROOT}/include/seeta"
    )
else()
    include_directories(
        "${OPENCV_ROOT}/include"
        "${OPENCV_ROOT}/include/opencv2"
        "${SEETAFACE_ROOT}/include"
        "${SEETAFACE_ROOT}/include/seeta"
    )
endif()

# 添加库文件目录
if(CMAKE_CROSSCOMPILING)
    link_directories(
        "${OPENCV_ROOT}/lib"
        "${SEETAFACE_ROOT}/lib"
        "${NFS_ROOT}/lib"
    )
else()
    link_directories(
        "${OPENCV_ROOT}/x64/mingw/lib"
        "${SEETAFACE_ROOT}/lib"
    )
endif()

# 查找库文件
if(CMAKE_CROSSCOMPILING)
    # 交叉编译环境下的库文件
    file(GLOB OPENCV_LIBS
        "${OPENCV_ROOT}/lib/libopencv_*.so"
        "${OPENCV_ROOT}/lib/libopencv_*.a"
        "${NFS_ROOT}/lib/libopencv_world.so"
    )
    file(GLOB SEETAFACE_LIBS
        "${SEETAFACE_ROOT}/lib/libSeeta*.so"
        "${SEETAFACE_ROOT}/lib/libSeeta*.a"
    )
else()
    # Windows环境下的库文件
    file(GLOB OPENCV_LIBS
        "${OPENCV_ROOT}/x64/mingw/lib/libopencv_*.dll.a"
        "${OPENCV_ROOT}/x64/mingw/lib/libopencv_*.a"
        "${OPENCV_ROOT}/x64/mingw/lib/libopencv_*.lib"
    )
    file(GLOB SEETAFACE_LIBS
        "${SEETAFACE_ROOT}/lib/libSeeta*.dll.a"
        "${SEETAFACE_ROOT}/lib/libSeeta*.a"
        "${SEETAFACE_ROOT}/lib/libSeeta*.lib"
    )
endif()

# 链接库文件
target_link_libraries(FaceAttendence PRIVATE
    Qt${QT_VERSION_MAJOR}::Widgets
    Qt${QT_VERSION_MAJOR}::Network
)

# 根据环境链接不同的库
if(CMAKE_CROSSCOMPILING)
    # 交叉编译环境
    target_link_libraries(FaceAttendence PRIVATE ${OPENCV_LIBS})
    target_link_libraries(FaceAttendence PRIVATE ${SEETAFACE_LIBS})
    
    # 添加系统库
    target_link_libraries(FaceAttendence PRIVATE
        pthread
        dl
        rt
    )
else()
    # Windows环境
    target_link_libraries(FaceAttendence PRIVATE
        ${OPENCV_LIBS}
        ${SEETAFACE_LIBS}
    )
endif()

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
if(${QT_VERSION} VERSION_LESS 6.1.0)
  set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.FaceAttendence)
endif()
set_target_properties(FaceAttendence PROPERTIES
    ${BUNDLE_ID_OPTION}
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

include(GNUInstallDirs)
install(TARGETS FaceAttendence
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(FaceAttendence)
endif()

# 交叉编译时的额外配置
if(CMAKE_CROSSCOMPILING)
    # 设置运行时库路径
    set_target_properties(FaceAttendence PROPERTIES
        INSTALL_RPATH "${NFS_ROOT}/lib"
        BUILD_WITH_INSTALL_RPATH TRUE
    )
    
    # 复制必要的模型文件到构建目录
    add_custom_command(TARGET FaceAttendence POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
        "${PACKAGE_ROOT}/opencv-4.5.2/share/opencv4/haarcascades/haarcascade_frontalface_alt2.xml"
        "${CMAKE_CURRENT_BINARY_DIR}/haarcascade_frontalface_alt2.xml"
        COMMENT "Copying OpenCV cascade file"
    )
endif()
